package com.server.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.service.ICommonDataService;
import com.server.service.IManagerAuthService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("ManagerAuthService")
public class ManagerAuthService implements IManagerAuthService {
	
	private static final String VIEW_NAME = "reward.v_user_employ";
	private static final String TABLE_NAME_EMPLOY = "reward.employ";
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	 /**
	 * 添加管理员
	 * @param employIds
	 * @param level
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月10日
	 */
	@Override
	public Boolean add(String employIds, Integer level) {
		String sql = "update %s set Manager=%s where Id in(%s)";
		return jdbcTemplate.update(String.format(sql, TABLE_NAME_EMPLOY,level,employIds)) > 0;
	}
	/**
	 * 删除管理员
	 * @param employIds
	 * @param level
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月10日
	 */
	@Override
	public Boolean delete(String employIds) {
		String sql = "update %s set Manager=0 where Id in(%s) and CardID not in(select CardID from reward.user where LoginName='admin')";
		int update = jdbcTemplate.update(String.format(sql, TABLE_NAME_EMPLOY,employIds));
		return update > 0;
	}

	 /**
	 * 管理员列表
	 * @param requestInput
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月10日
	 */
	@Override 
	public DataVector<Map<String, Object>> getList(RequestInput requestInput){
		String whereStr = "State=1 and Manager in (7,8,9)";
		String sortStr = "Manager desc,OrgNo asc,EmployTime desc";
		return commonDataService.getListData(VIEW_NAME, whereStr, sortStr,
				requestInput);
	}
	
}
